package com.课上案例练习.线程.线程池;

import java.util.concurrent.*;

import static java.util.concurrent.TimeUnit.SECONDS;

public class Demo1 {
    public static void main(String[] args) {
             //实例化一个人任务对象
        MyThread myThread = new MyThread();
        //创建一个阻塞队列
        LinkedBlockingQueue<Runnable> queue = new LinkedBlockingQueue<>();
        queue.add(()->{
            System.out.println("线程池执行任务");
        });
        //准备一个拒绝策略对象
        //直接拒绝策略，抛出异常
        RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
        //创建一个自定义线程池对象
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3,
                5,
                10,
               TimeUnit.SECONDS,
                queue,
                handler);


        threadPoolExecutor.submit(myThread);
        threadPoolExecutor.submit(myThread);
        threadPoolExecutor.submit(myThread);
        threadPoolExecutor.submit(myThread);
        threadPoolExecutor.submit(myThread);
        threadPoolExecutor.submit(myThread);
        threadPoolExecutor.submit(myThread);
        threadPoolExecutor.submit(myThread);
        threadPoolExecutor.submit(myThread);
        threadPoolExecutor.shutdown();
    }
}
class MyThread implements Runnable{

    @Override
    public void run() {
        for (int i = 1; i <= 10; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+":i的值是："+i);
        }
    }
}